package com.example.demo.util

import io.vertx.core.http.HttpHeaders
import io.vertx.core.http.HttpServerResponse
import io.vertx.core.json.JsonObject

/**
 * @author Xu Haidong
 * @date 2023/4/21 14:17
 */
class JsonResult(code: Int, msg: String, data: Any? = null) : JsonObject() {

  init {
    this.put("code", code).put("msg", msg).put("data", data)
  }
}

fun HttpServerResponse.successJson(data: Any? = null) {
  this.putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
  this.end(JsonResult(200, "success", data).encodePrettily())
}

fun HttpServerResponse.failJson() {
  this.putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8")
  this.end(JsonResult(500, "failed").encodePrettily())
}
